package concurrence.cas;

import java.util.concurrent.atomic.AtomicReference;
//Cas应用在单个对象上
public class AtomicReferenceDemo {
    public static void main(String[] args) {
        User zsan = new User("zsan",20);
        User lisi = new User("lisi",21);
        AtomicReference<User> userAtomicReference = new AtomicReference<>();
        userAtomicReference.set(zsan);//A线程工作内存中共享变量的拷贝值
        System.out.println(userAtomicReference.compareAndSet(zsan, lisi)+"\t"+userAtomicReference.get());
        System.out.println(userAtomicReference.compareAndSet(zsan, lisi)+"\t"+userAtomicReference.get());
    }
}

class User {

    String userName;
    int age;

    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    @Override
    public String toString() {
        return String.format("User [userName=%s, age=%s]", userName, age);
    }
}
